package com.scrawler.util;


/**
*   
*   @Description	load config file and read path interface    
*   @creator         tangkun
*   @create-time     2011-8-3
*   @revision        $Id
*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoadConfig {
	
	private static final Log LOG = LogFactory.getLog("projectlog");
	private static Properties property = null;
	private static final LoadConfig CONFIG = new LoadConfig();
	public static final String OS_NAME;
	
	static
	{
		String osStr = System.getProperty("os.name");
		if(osStr.equalsIgnoreCase("linux"))
		{
			OS_NAME = "linux";
		}
		else
		{
			OS_NAME = "windows";
		}
		property = new Properties();
		FileInputStream fileStream = null;
		String basePath =PathUtil.getInstance().getBasePath();
		String path = null;
		try {
				 path = basePath+ConstantUtils.CONFIG_FILE_PROJECT;
				LOG.info(path);
			 	fileStream = new FileInputStream(path);
			 	property.load(fileStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			LOG.error("project config file:"+path+"not found...");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			LOG.error("load project config error......",e);
		}	
		LOG.info("load /config/config.properties successs........");
	}
	
	//singleton
	private LoadConfig(){}
	
	/**
	 * lookUp path by key
	 * @param key
	 * @return value mapping with key
	 */
	public String lookUpValueByKey(String key)
	{
		if(!property.containsKey(key))
		{
			try {
				throw new NoSuchFieldException(key);
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				LOG.info("NoSuchFieldException: "+key,e);
			}
		}
		String value = (String) property.get(key);
		return value.trim();
	}
	
	/**
	 * @return os name
	 */
	public static String getPrefix()
	{
		return OS_NAME;
	}
	
	public static  LoadConfig getInstance()
	{
		return CONFIG;
	}

	public static void main(String args[])
	{	
		String value = getInstance().lookUpValueByKey("nba_address");
		System.out.println(value);
	}
}
